package com.jzo2o.customer.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.common.expcetions.CommonException;
import com.jzo2o.common.utils.BeanUtils;
import com.jzo2o.customer.enums.AuditStatusEnum;
import com.jzo2o.customer.enums.CertificationStatusEnum;
import com.jzo2o.customer.mapper.WorkerCertificationAuditorMapper;
import com.jzo2o.customer.mapper.WorkerCertificationMapper;
import com.jzo2o.customer.model.domain.AgencyCertification;
import com.jzo2o.customer.model.domain.AgencyCertificationAuditor;
import com.jzo2o.customer.model.domain.WorkerCertification;
import com.jzo2o.customer.model.domain.WorkerCertificationAuditor;
import com.jzo2o.customer.model.dto.WorkerCertificationUpdateDTO;
import com.jzo2o.customer.model.dto.request.WorkerCertificationAuditAddReqDTO;
import com.jzo2o.customer.model.dto.response.WorkerCertificationResDTO;
import com.jzo2o.customer.service.IWorkerCertificationAuditorService;
import com.jzo2o.customer.service.IWorkerCertificationService;
import com.jzo2o.mvc.utils.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 服务人员认证信息表 服务实现类
 * </p>
 *
 * @author itcast
 * @since 2023-09-06
 */
@Service
public class WorkerCertificationServiceImpl extends ServiceImpl<WorkerCertificationMapper, WorkerCertification> implements IWorkerCertificationService {


    @Autowired
    IWorkerCertificationAuditorService workerCertificationAuditorService;

    @Autowired
    WorkerCertificationAuditorMapper workerCertificationAuditorMapper;

    /**
     * 根据服务人员id更新
     *
     * @param workerCertificationUpdateDTO 服务人员认证更新模型
     */
    @Override
    public void updateById(WorkerCertificationUpdateDTO workerCertificationUpdateDTO) {
        LambdaUpdateWrapper<WorkerCertification> updateWrapper = Wrappers.<WorkerCertification>lambdaUpdate()
                .eq(WorkerCertification::getId, workerCertificationUpdateDTO.getId())
                .set(WorkerCertification::getCertificationStatus, workerCertificationUpdateDTO.getCertificationStatus())
                .set(ObjectUtil.isNotEmpty(workerCertificationUpdateDTO.getName()), WorkerCertification::getName, workerCertificationUpdateDTO.getName())
                .set(ObjectUtil.isNotEmpty(workerCertificationUpdateDTO.getIdCardNo()), WorkerCertification::getIdCardNo, workerCertificationUpdateDTO.getIdCardNo())
                .set(ObjectUtil.isNotEmpty(workerCertificationUpdateDTO.getFrontImg()), WorkerCertification::getFrontImg, workerCertificationUpdateDTO.getFrontImg())
                .set(ObjectUtil.isNotEmpty(workerCertificationUpdateDTO.getBackImg()), WorkerCertification::getBackImg, workerCertificationUpdateDTO.getBackImg())
                .set(ObjectUtil.isNotEmpty(workerCertificationUpdateDTO.getCertificationMaterial()), WorkerCertification::getCertificationMaterial, workerCertificationUpdateDTO.getCertificationMaterial())
                .set(ObjectUtil.isNotEmpty(workerCertificationUpdateDTO.getCertificationTime()), WorkerCertification::getCertificationTime, workerCertificationUpdateDTO.getCertificationTime());
        super.update(updateWrapper);
    }

    @Override
    public void submit(WorkerCertificationAuditAddReqDTO reqDTO) {
        //判断是否第一次认证
        WorkerCertification one = lambdaQuery().eq(WorkerCertification::getId, UserContext.currentUserId()).one();
        if (ObjectUtil.isEmpty(one)) {
            //第一次认证，新增数据
            //添加认证表
            WorkerCertification workerCertification = BeanUtils.copyBean(reqDTO, WorkerCertification.class);
            workerCertification.setCertificationStatus(CertificationStatusEnum.PROGRESSING.getStatus());
            workerCertification.setId(UserContext.currentUserId());
            int flag1 = baseMapper.insert(workerCertification);

            //添加审核表
            WorkerCertificationAuditor workerCertificationAuditor = new WorkerCertificationAuditor();
            workerCertificationAuditor.setId(UserContext.currentUserId());
            workerCertificationAuditor.setAuditStatus(AuditStatusEnum.UNREVIEWED.getStatus());
            int flag2 = workerCertificationAuditorMapper.insert(workerCertificationAuditor);

            if (flag1 != 1 || flag2 != 1) {
                throw new CommonException("提交认证失败");
            }
        } else {
            //重新认证
            if (!one.getCertificationStatus().equals(CertificationStatusEnum.FAIL.getStatus())) {
                throw new CommonException("重新认证失败，状态不正确");
            }
            //修改认证表
            WorkerCertification workerCertification = BeanUtils.copyBean(reqDTO, WorkerCertification.class);
            workerCertification.setCertificationStatus(CertificationStatusEnum.PROGRESSING.getStatus());
            workerCertification.setId(UserContext.currentUserId());
            int flag1 = baseMapper.updateById(workerCertification);
            //修改审核表
            WorkerCertificationAuditor workerCertificationAuditor = new WorkerCertificationAuditor();
            workerCertificationAuditor.setAuditStatus(AuditStatusEnum.UNREVIEWED.getStatus());
            workerCertificationAuditor.setId(UserContext.currentUserId());
            int flag2 = workerCertificationAuditorMapper.updateById(workerCertificationAuditor);
            if (flag1 != 1 || flag2 != 1) {
                throw new CommonException("重新认证失败");
            }
        }
    }
}
